package com.jiaquyun.jcyx.jetpack.mode

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.SPStaticUtils
import com.jiaquyun.jcyx.app.AppConstance
import com.jiaquyun.jcyx.base.BaseViewModel
import com.jiaquyun.jcyx.entity.*
import com.jiaquyun.jcyx.jetpack.api.UserRepository
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.SubscribeViewModel

class MineViewModel(application: Application) : BaseViewModel(application) {

    private val mUserRepository = UserRepository()

    val userIndexLiveDate = MutableLiveData<HttpResponseBody<UserIndexResponse>>()
    fun userIndex() {
        setRxLifeScope(object : SubscribeViewModel<UserIndexResponse>(userIndexLiveDate) {
            override fun onSuccess(resp: UserIndexResponse) {
                super.onSuccess(resp.apply { keepUserIndex(data) })
            }

            /**
             * 保存用户信息
             * */
            private fun keepUserIndex(userInfo: UserIndex) {
                SPStaticUtils.put(AppConstance.SPS_KEY_USER_INDEX, userInfo.toString())
            }
        }) {
            mUserRepository.userIndex()
        }
    }

    val userShopsLiveDate = MutableLiveData<HttpResponseBody<UserShopsResponse>>()
    fun userShopsList(lng: String, lat: String) {
        setRxLifeScope(SubscribeViewModel(userShopsLiveDate)) {
            mUserRepository.userShopsList(lng, lat)
        }
    }

    val userSignListLiveDate = MutableLiveData<HttpResponseBody<UserSignListResponse>>()
    fun userScoreSignList() {
        setRxLifeScope(SubscribeViewModel(userSignListLiveDate)) {
            mUserRepository.userScoreSignList()
        }
    }

    val userSignLiveDate = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userScoreSign() {
        setRxLifeScope(SubscribeViewModel(userSignLiveDate)) {
            mUserRepository.userScoreSign()
        }
    }


    var page: Int = 1
    var rows: Int = 20

    val userScoreLogLiveDate = MutableLiveData<HttpResponseBody<UserScoreResponse>>()
    fun userScoreLog(type: Int, isLoadMore: Boolean = false) {
        val listener = object : SubscribeViewModel<UserScoreResponse>(userScoreLogLiveDate) {
            override fun onSuccess(resp: UserScoreResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        setRxLifeScope(listener) {
            mUserRepository.userScoreLog(type.toString(), page.toString(), rows.toString())
        }
    }

    val userCouponListLiveDate = MutableLiveData<HttpResponseBody<CouponListResponse>>()
    fun userCouponList(status: Int, isLoadMore: Boolean = false) {
        val listener = object : SubscribeViewModel<CouponListResponse>(userCouponListLiveDate) {
            override fun onSuccess(resp: CouponListResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        setRxLifeScope(listener) {
            mUserRepository.userCouponList(status.toString(), page.toString(), rows.toString())
        }
    }

    val userMoneyLogLiveDate = MutableLiveData<HttpResponseBody<UserMoneyLogResponse>>()
    fun userMoneyLog(isLoadMore: Boolean = false) {
        val listener = object : SubscribeViewModel<UserMoneyLogResponse>(userMoneyLogLiveDate) {
            override fun onSuccess(resp: UserMoneyLogResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        setRxLifeScope(listener) {
            mUserRepository.userMoneyLog(page.toString(), "500")
        }
    }

    val userYongApplyLiveDate = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userYongApply(money: String) {
        setRxLifeScope(SubscribeViewModel(userYongApplyLiveDate)) {
            mUserRepository.userYongApply(money)
        }
    }

    val userYongLogLiveDate = MutableLiveData<HttpResponseBody<UserYongLogResponse>>()
    fun userYongLog(isLoadMore: Boolean = false) {
        val listener = object : SubscribeViewModel<UserYongLogResponse>(userYongLogLiveDate) {
            override fun onSuccess(resp: UserYongLogResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        setRxLifeScope(listener) {
            mUserRepository.userYongLog(page.toString(), "500")
        }
    }

    val userInvoiceHistoryLiveDate = MutableLiveData<HttpResponseBody<UserInvoiceHistoryResponse>>()
    fun userInvoiceHistory(isLoadMore: Boolean = false) {
        val listener = object : SubscribeViewModel<UserInvoiceHistoryResponse>(userInvoiceHistoryLiveDate) {
            override fun onSuccess(resp: UserInvoiceHistoryResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        setRxLifeScope(listener) {
            mUserRepository.userInvoiceHistory(page.toString(), "500")
        }
    }

    val userInvoiceOrderLiveDate = MutableLiveData<HttpResponseBody<UserInvoiceOrderResponse>>()
    fun userInvoiceOrder(isLoadMore: Boolean = false) {
        val listener = object : SubscribeViewModel<UserInvoiceOrderResponse>(userInvoiceOrderLiveDate) {
            override fun onSuccess(resp: UserInvoiceOrderResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        setRxLifeScope(listener) {
            mUserRepository.userInvoiceOrder(page.toString(), "500")
        }
    }

    val userInvoiceCreateLiveDate = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userInvoiceCreate(create: UserInvoiceCreate) {
        setRxLifeScope(SubscribeViewModel(userInvoiceCreateLiveDate)) {
            mUserRepository.userInvoiceCreate(create)
        }
    }

    val userInvoiceQueryLiveDate = MutableLiveData<HttpResponseBody<UserInvoiceQueryResponse>>()
    fun userInvoiceQuery(invoice_no: String) {
        setRxLifeScope(SubscribeViewModel(userInvoiceQueryLiveDate)) {
            mUserRepository.userInvoiceQuery(invoice_no)
        }
    }
}